home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
lantimes
/
90_02
/
get.c
next >
Wrap
Text File
|
1989-12-11
|
3KB
|
106 lines
/*-------------------------------------------------------------------------*
* get.c -- get a file from remote node
*
* Usage: get \\nodeid
* get \\nodeid filename.ext
*
* The remote node must also run 'put' to complete the transfer.
* \\nodeid and filename.ext are command line arguments
*
* Compiler: Microsoft C Version 5.0.
*
* Tom Nolan 7/12/89
*
*-------------------------------------------------------------------------*/
#include <stdio.h>
#include <io.h>
#include "netbios.h"
void nreceive(char *node, char *fname);
void usage(void);
typedef struct /* network information block definition */
{
char fname[MAXPATH]; /* file name for transmission */
long fsize; /* size of file in bytes */
char sender[NODE_NAME_LEN]; /* name of sending node */
char receiver[NODE_NAME_LEN]; /* name of receiving node */
}
NETINFO;
char buf[4096]; /* buffer for send/receive data */
#define diag printf /* remove "printf" to turn off diags */
/*-------------------------------------------------------------------------*/
main(int argc, char **argv)
{
if(argc == 2 && isnodename(argv[1]))
nreceive(argv[1], "");
else
if(argc == 3 && isnodename(argv[1]) && !isnodename(argv[2]))
nreceive(argv[1], argv[2]);
else
usage();
exit(0);
}
/*-------------------------------------------------------------------------*/
void nreceive(char *node, char *fname) /* network receive */
{
NETINFO netinfo; /* network information block */
int lsn; /* logical session number */
int len; /* length of received data */
char name[NODE_NAME_LEN]; /* remote node name */
FILE *fp; /* file stream pointer */
char *fn; /* file name pointer */
do /* receive a datagram */
{
diag("Receiving datagram\n"); /* from requested node... */
recv_datagram(&netinfo, sizeof(netinfo), name); /* if sender isn't */
} /* correct one, */
while(strcmp(name, node)); /* ...try again */
strcpy(netinfo.sender, node); /* fill in sender's name */
diag("Sending datagram\n"); /* and return the datagram */
send_datagram(netinfo.sender, &netinfo, sizeof(netinfo));
lsn = listen(netinfo.sender, netinfo.receiver); /* wait for call */
if(!lsn)
{
printf("Error establishing link to %s\n",node);
exit(1);
}
fn = (!*fname) ? netinfo.fname : fname; /* file name to create */
diag("Communicating on LSN %d\n",lsn);
fp = fopen(fn, "wb"); /* create received file */
printf("Packets:");
while(netinfo.fsize > 0) /* do for the whole file */
{
diag(".");
len = receive(lsn, buf, sizeof(buf)); /* receive a buffer */
fwrite(buf, 1, len, fp); /* write it to the file */
netinfo.fsize -= len; /* track bytes remaining */
}
hangup(lsn); /* terminate session */
fclose(fp); /* and close file */
printf("\nReceived file %s\n", fn);
}
/*-------------------------------------------------------------------------*/
void usage(void)
{
printf( "Get v1.00 T. Nolan - 7/12/89\n"
"usage:\n\n"
"get \\nodeid filename.ext - get filename.ext from nodeid\n"
"get \\nodeid - get using sender's file name\n\n"
"Get and put must be run on each node to transfer a file.\n");
exit(1);
}